package cn.hdu.liang.core.controller.admin;

import cn.hdu.liang.core.bean.order.Detail;
import cn.hdu.liang.core.bean.order.Order;
import cn.hdu.liang.core.bean.user.Addr;
import cn.hdu.liang.core.query.order.DetailQuery;
import cn.hdu.liang.core.query.order.OrderQuery;
import cn.hdu.liang.core.query.user.AddrQuery;
import cn.hdu.liang.core.service.order.DetailService;
import cn.hdu.liang.core.service.order.OrderService;
import cn.hdu.liang.core.service.user.AddrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 订单列表
 * 订单查看
 */
@Controller
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private DetailService detailService;
    @Autowired
    private AddrService addrService;

    //订单列表
    @RequestMapping(value = "/order/list.do")
    public String list(Integer isPaiy, Integer state, ModelMap model) {
        OrderQuery orderQuery = new OrderQuery();
        if(isPaiy != null) {
            orderQuery.setIsPaiy(isPaiy);
        }
        if(state != null) {
            orderQuery.setState(state);
        }
        List<Order> orders = orderService.getOrderList(orderQuery);
        model.addAttribute("orders", orders);

        return "order/list";
    }

    //订单查看
    @RequestMapping(value = "/order/view.do")
    public String view(Integer id, ModelMap model) {
        //查询订单主表
        Order order = orderService.getOrderByKey(id);
        //查询订单详情表
        DetailQuery detailQuery = new DetailQuery();
        detailQuery.setOrderId(id);
        List<Detail> details = detailService.getDetailList(detailQuery);

        //收货地址
        AddrQuery addrQuery = new AddrQuery();
        addrQuery.setBuyerId(order.getBuyerId());
        addrQuery.setIsDef(1);

        List<Addr> addrs = addrService.getAddrList(addrQuery);

        model.addAttribute("order", order);
        model.addAttribute("addr", addrs.get(0));
        model.addAttribute("details", details);
        return "order/view";
    }
}
